﻿using DevExpress.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XCI.Core;

namespace XCI.Windows
{
    public class InfinitePage<T> where T : class, new()
    {
        private VirtualServerModeSource _virtualServerModeSource;

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 页数,从1开始
        /// </summary>
        public int PageIndex { get; set; }

        public DevExpress.XtraGrid.GridControl GridControl { get; set; }

        public event EventHandler<DataSourceEventArgs> LoadDataSource;
        public event EventHandler<PageInfoEventArgs> ShowPageInfo;

        public InfinitePage(DevExpress.XtraGrid.GridControl gridControl, int pageSize = 50, int pageIndex = 1)
        {
            PageSize = pageSize;
            PageIndex = pageIndex;
            GridControl = gridControl;

            _virtualServerModeSource = new VirtualServerModeSource(typeof(T));
            _virtualServerModeSource.ConfigurationChanged += _virtualServerModeSource_ConfigurationChanged;
            _virtualServerModeSource.MoreRows += _virtualServerModeSource_MoreRows;

            gridControl.DataSource = _virtualServerModeSource;
        }

        public string FormatPageInfo(PageInfo pageInfo)
        {
            if (pageInfo.TotalCount==0)
            {
                return $"共 {pageInfo.TotalCount} 条记录";
            }
            return $"已加载 {pageInfo.RecordEndIndex} 条记录,共 {pageInfo.TotalCount} 条记录";
        }

        private void _virtualServerModeSource_MoreRows(object sender, VirtualServerModeRowsEventArgs e)
        {
            if (LoadDataSource == null)
            {
                return;
            }
            PageIndex++;
            var paged = new PageParam(PageIndex,PageSize);
            if (e.ConfigurationInfo.SortInfo?.Length > 0)
            {
                var sortInfo = e.ConfigurationInfo.SortInfo[0];
                paged.SortName = sortInfo.SortPropertyName;
                paged.SortDir = sortInfo.IsDesc ? "desc" : "asc";
            }
            e.RowsTask = Task.Factory.StartNew(() =>
            {
                var args = new DataSourceEventArgs { PageParam = paged };
                LoadDataSource.Invoke(this, args);
                if (args.PageList == null)
                {
                    return new VirtualServerModeRowsTaskResult(null, false);
                }
                var pageList = (PageList<T>)args.PageList;
                var pageInfo = new PageInfo(pageList.PageIndex, pageList.PageSize, pageList.Total);
                var hasMoreRows = pageInfo.HasNextPage;
                ShowPageInfo?.Invoke(this, new PageInfoEventArgs { PageInfo = pageInfo });
                return new VirtualServerModeRowsTaskResult(pageList.Rows, hasMoreRows);
            }, e.CancellationToken);
        }

        private void _virtualServerModeSource_ConfigurationChanged(object sender, VirtualServerModeRowsEventArgs e)
        {
            PageIndex = 0;
        }
    }

    public class DataSourceEventArgs : EventArgs
    {
        public PageParam PageParam { get; set; }
        public object PageList { get; set; }
        public PageInfo PageInfo<T>(PageList<T> plist)
        {
            return new PageInfo(plist.PageIndex, plist.PageSize, plist.Total);
        }
    }
    public class PageInfoEventArgs : EventArgs
    {
        public PageInfo PageInfo { get; set; }
    }
}
